/////////////////////////////////////////////////////////////////////////////////////
// Header Protection
#ifndef _MUTEX_H_
#define _MUTEX_H_

/////////////////////////////////////////////////////////////////////////////////////
// Include Files
#include <Windows.h>

/////////////////////////////////////////////////////////////////////////////////////
// A critical section which can only be locked by one thread at a time.
class mutex_t
{
public:     // methods

	mutex_t() { InitializeCriticalSection(&m_hMutex); }
	~mutex_t() { DeleteCriticalSection(&m_hMutex); }

	// Locks the mutex, if it is already locked on another thread, this method will 
	//     wait until it is unlocked before returning.
	void Lock() { EnterCriticalSection(&m_hMutex); }

	// Attempts to lock the mutex, if it is already locked on another thread, this 
	//     method will return false.
	// return: bool_t - whether or not the lock attempt was successful.
	bool_t TryLock() { return TryEnterCriticalSection(&m_hMutex) != 0; }

	// Unlocks the mutex so it can be locked by another thread.
	void Unlock() { LeaveCriticalSection(&m_hMutex); }

	// HACK: these are just creating new mutexes, unless I can ref count a mutex or something

	mutex_t( mutex_t const &_rhs ) { (void)_rhs; InitializeCriticalSection(&m_hMutex); }
	mutex_t &operator =( mutex_t const &_rhs ) { if( this != &_rhs ) { InitializeCriticalSection(&m_hMutex); } return *this; }

private:    // data
	CRITICAL_SECTION m_hMutex;
};

/////////////////////////////////////////////////////////////////////////////////////
// Header Protection
#endif // _MUTEX_H_
